import { MonthPredictionI } from "@/models/prediction";
import React, { useMemo, useState } from "react";
import { convertEventTime, EVENT_DATA } from "@/pages/gantt/helpers";
import { EventI } from "@/models/events";
import { useUsers } from "@/api/users";

type PredictionTableDataI = {
  id: string;
  week: number;
  year: number;
  month: number;
  month_id: number;
  status: string;
  specializations: {
    id: number;
    qtty: number;
    qtty_predicted: number;
  }[];
};

export const normalizeNewEventsData = (
  row: MonthPredictionI
): PredictionTableDataI[] => {
  const weeks: number[] = [];
  row.weeks.forEach((w) => {
    if (!weeks.includes(w.week)) weeks.push(w.week);
  });
  const res = []
    .concat(weeks)
    .reverse()
    .map((w, index) => {
      const weekData = row.weeks
        .filter((item) => item.week === w)
        .map((item) => {
          return {
            id: item.specialization.id,
            qtty: item.qtty,
            qtty_predicted: item.qtty_predicted,
          };
        });
      return {
        id: `week${w}month${row.month}year${row.year}`,
        week: w,
        year: row.year,
        month: row.month,
        month_id: row.id,
        status: index === 0 ? row.status : "",
        specializations: weekData,
      };
    });
  return res;
};

export const MONTHS = [
  "",
  "Январь",
  "Февраль",
  "Март",
  "Апрель",
  "Май",
  "Июнь",
  "Июль",
  "Август",
  "Сентябрь",
  "Октябрь",
  "Ноябрь",
  "Декабрь",
];

const dateOptions: Intl.DateTimeFormatOptions = {
  hour: "numeric",
  minute: "numeric",
  month: "long",
  day: "numeric",
  year: "numeric",
};
const params = new URLSearchParams({ limit: "99999", offset: "0" });

export const useNewEventsColumns = () => {
  const [, users] = useUsers(params);
  return useMemo(() => {
    return [
      {
        name: "Врач",
        key: "user",
        cell: (data: EventI) => {
          const user = users?.find((i) => i.id === data.user);
          if (!user) return "";
          return (
            <div>
              {user.first_name} {user.last_name}
            </div>
          );
        },
        width: 15,
      },
      {
        name: "Начало",
        key: "start_time",
        cell: (data: EventI) => {
          const t = convertEventTime(data);
          return <div>{new Date(t).toLocaleString("ru-RU", dateOptions)}</div>;
        },
        width: 15,
      },
      {
        name: "Конец",
        key: "end_time",
        cell: (data: EventI) => {
          const t = convertEventTime(data, true);
          return <div>{new Date(t).toLocaleString("ru-RU", dateOptions)}</div>;
        },
        width: 15,
      },
      {
        name: "Тип события",
        key: "kind",
        cell: (data: EventI) => {
          return <div>{EVENT_DATA[data.kind].title}</div>;
        },
        width: 15,
      },
    ];
  }, [users]);
};

export const getMonthIdByStatus = (
  arr: MonthPredictionI[]
): MonthPredictionI => {
  const tempIndex = arr.findIndex((i) => i.status === "temporary");
  const approvedIndex = arr.findIndex((i) => i.status === "approved");
  const nApprovedIndex = arr.findIndex((i) => i.status === "not_approved");

  if (tempIndex !== -1) {
    return arr[tempIndex];
  }
  if (approvedIndex !== -1) {
    return arr[approvedIndex];
  }
  if (nApprovedIndex !== -1) {
    return arr[nApprovedIndex];
  }
  return null;
};
